<template>
  <LowCodeLayout :defaultMenu="defaultMenu" ref="lowCodeLayout" :is-dev="isDev"
                 :app-code="appCode" :level-list.sync="levelList" @releaseApp="releaseApp"
                 @visitorApp="visitorApp">
    <applicationSystem v-if="menuId" :is-dev="isDev" :isDefaultMenu="isDefaultMenu"
                       :app-code="appCode" :menu-id="menuId" />
    <newBlankApp v-if="!menuId&&isDev" @openDialog="openDialog" />
  </LowCodeLayout>
</template>

<script>
import LowCodeLayout from '@/framework/views/developer-center/layout/index.vue'
import applicationSystem from './application-system/index.vue'
import newBlankApp from './components/newBlankApp.vue'
import appListApi from '@/framework/api/developerCenter/application-center/applicationCenter'

export default {
  name: 'ReviewTemplate',
  components: { LowCodeLayout, applicationSystem, newBlankApp },
  data() {
    return {
      defaultMenu: ['myProcess', 'myApply', 'myTodo', 'myDone', 'myCopy', 'systemLog'], // 默认菜单的appCode
      isDev: false,
      menuId: '',
      appCode: '',
      levelList: [],
      isDefaultMenu: false
    }
  },
  watch: {
    $route: {
      handler(newVal) {
        const { menuCode, menuId } = this.$route.query
        this.menuId = menuId
        menuCode && (this.isDefaultMenu = this.defaultMenu.includes(menuCode.substr(0, menuCode.length - 6)))
      },
      immediate: true
    }
  },
  created() {
    this.isDev = this.$route.path === '/designerTemplate'
    const { appCode } = this.$route.query
    this.appCode = appCode
  },
  methods: {
    getFirstMenu(arr) {
      for (let i = 0; i <= arr.length; i++) {
        if (!arr[i]) {
          continue
        }
        if (arr[i].menuType === 'C' && arr[i].statusFlag == 1) {
          return arr[i]
        }
        if (arr[i].children && arr[i].children.length) {
          const temp = this.getFirstMenu(arr[i].children)
          if (temp) { return temp }
        }
      }
    },
    // 发布应用
    releaseApp() {
      const { appId, statusFlag } = this.$route.query
      appListApi.updateStatus({ appId, statusFlag: 1 }).then(() => {
        this.$dtModal.msgSuccess('发布成功')
      }).catch(() => {})
    },
    openDialog(param) {
      this.$refs.lowCodeLayout.openSidebarAddForm(param)
    },
    // 访问应用
    visitorApp() {
      const temp = this.getFirstMenu(this.$store.state.developerCenter.menuList) || {}
      const { menuId, menuName, formId, menuCode } = temp
      const { appName, appId, statusFlag } = this.$route.query
      const routeUrl = this.$router.resolve({
        path: '/visitorTemplate',
        query: {
          appCode: this.appCode,
          menuId,
          menuCode,
          appName,
          appId,
          menuName,
          formId
        }
      })
      window.open(routeUrl.href, '_blank')
    }
  }
}
</script>

<style type="text/css" scoped>
</style>
